<html>
<body>

<h1>Calculator</h1>
by Samuel Jun (003-955-212)<br />
for CS143 Project 1A<br />
<br />

<form method="get">
<input type="text" name="expr" />
<input type="submit" value="Calculate" />
</form>

<?php

$input = $_GET["expr"];

error_reporting(0);

if (!empty($input))
{
    if (preg_match('#[^0-9\+\-\*\/\.]#', $input) || // not 0-9 + - * / .
        preg_match('#^[\.\*\/]#', $input)        || // start . * /
        preg_match('#[\+\-\*\/\.]$#', $input)    || // end + - * / .
        preg_match('#[\+\*\/]{2,}#', $input)     || // two + * /  or more
        preg_match('#\.{2,}#', $input)           || // two . or more
        preg_match('#[\-]{3,}#', $input)         || // three - or more
        preg_match('#\/0[\+\-\*\/]+#', $input)   || // /0 without 0-9 .
        preg_match('#\/0[\.]*$#', $input)        ||
        preg_match('#[^0-9]\.[^0-9]#', $input)   
       )
    {
        echo "Invalid expression: " . $input . "<br />";
    }
    else
    {
        $newInput = preg_replace('#\-\-#', '+', $input);
        eval("\$result = $newInput;");
        echo "<b>Result:</b> " . $input . " = " . $result . "<br />";
    }
}

?>

</body>
</html>
